
/**
 * AppleIIGo
 * RamWorks class
 * (C) 2007-2008 by Marc S. Ressl (mressl@umich.edu)
 * Released under the GPL
 */

#include "RamWorks.h"

UINT8 ramWorksBankMap[] = {
	0x00
};

RamWorks::RamWorks(Motherboard * motherboard) :
AuxSlotDevice(motherboard) {
	bankNum = 1;
	bankMap = ramWorksBankMap;
}

void RamWorks::onDevicesConnected() {
	AuxSlotDevice::onDevicesConnected();
	mmu = motherboard->getMmu();
}

void RamWorks::onMemoryRequested() {
	memoryOffset =
		motherboard->getMemory()->requestMemory(bankNum * BANK_BYTENUM);
}

void RamWorks::onMemoryAllocated() {
	for (UINT32 index = 0; index < RAMWORKS_BANKSELECT_END; index++)
		bankSelectToMemoryOffset[index] = OFFSET_FLOATINGBUS;
	
	for (UINT32 index = 0; index < bankNum; index++)
		bankSelectToMemoryOffset[bankMap[index]] =
		memoryOffset + BANK_BYTENUM * index;
}

void RamWorks::onResetAsserted(bool isRestart) {
	if (!isRestart)
		return;
	
	UINT8 * memory = motherboard->getMemory()->getMemory(memoryOffset);
	for (UINT32 index = 0; index < (bankNum * BANK_BYTENUM); index++)
		memory[index] = 0;
	
	setBankSelect(0);
}

void RamWorks::writeIo(UINT16 address, UINT8 value) {
	if ((address & 0xf) == 0x3)
		setBankSelect(value);
}

UINT8 RamWorks::readIo(UINT16 address) {
	if ((address & 0xf) == 0x3)
		return bankSelect;
	
	return timingGenerator->readFloatingBus();
}

void RamWorks::setBankSelect(UINT8 value) {
	bankSelect = value;
	mmu->setAuxMemory(bankSelectToMemoryOffset[value], (bankSelect == 0));
}
